﻿using BusinessLayer.DatabaseEntities;
using BusinessLayer.VDataEntities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BusinessLayer
{
    public class CropBusiness
    {
        public static VCrop[] GetActiveCropList()
        {
            using (TrialManagementEntities trEntity = new DatabaseEntities.TrialManagementEntities())
            {
                var cropList = from crop in trEntity.Crop
                               where crop.IsActive == true
                               select new VCrop() { ID = crop.ID, Name = crop.Name, IsActive = crop.IsActive, IsOthers = crop.IsOthers };
                return cropList.ToArray();
            }
        }

        public static VCrop[] GetAllCropListWithoutOthers()
        {
            using(TrialManagementEntities trEntity = new TrialManagementEntities())
            {

                var cropList = from crop in trEntity.Crop
                               where crop.IsOthers == false
                               select new VCrop() { ID = crop.ID, Name = crop.Name, IsActive = crop.IsActive, IsOthers  = crop.IsOthers };
                return cropList.ToArray();
            }
        }

        public static void AddCrop(string Name)
        {
            using(TrialManagementEntities trEntity = new TrialManagementEntities())
            {
                trEntity.Crop.Add(new Crop() { Name = Name, IsActive = true, IsOthers = false });
                trEntity.SaveChanges();
            }
        }

        public static void ChangeStatus(long CropID)
        {
            using (TrialManagementEntities trEntity = new TrialManagementEntities())
            {
               Crop updatedCrop = (from crop in trEntity.Crop
                                   where crop.ID == CropID
                                   select crop).FirstOrDefault();
                if(updatedCrop != null)
                {
                    bool updatedStatus = !updatedCrop.IsActive;
                    updatedCrop.IsActive = updatedStatus;
                    trEntity.SaveChanges();
                }
            }
        }
    }
}
